package per.ace.fastjson.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.web.bind.annotation.*;
import per.ace.fastjson.entity.Order;
import per.ace.fastjson.vo.OrderVO;

import java.util.*;

/**
 * @program: ace-json-starter
 * @description: order web
 * @author: ace
 * @Date: 2020-03-27 11:31
 */
@RestController
@RequestMapping("order")
public class OrderController {

    @GetMapping()
    public List<Order> getOrder(){
        Order o = new Order();
        o.setOrderId(1L);
        o.setUserId(23L);
        o.setItemId(100009186366L);
        o.setItemName("微软 Surface Pro 7");
        o.setCreateTime(new Date());
        List l = new ArrayList();
        l.add(o);
        return l;
    }

    /**
     * RESTful 接口
     * @param orderId
     * @return
     */
    @GetMapping("{orderId}")
    public Order getOrderById(@PathVariable("orderId") Long orderId){
        Order o = new Order();
        o.setOrderId(orderId);
        o.setUserId(23L);
        o.setItemId(100009186366L);
        o.setItemName("微软 Surface Pro 7");
        o.setCreateTime(new Date());
        return o;
    }

    @GetMapping("/str")
    public String getOrderString(){
        Order o = new Order();
        o.setOrderId(1L);
        o.setUserId(23L);
        o.setItemId(100009186366L);
        o.setItemName("微软 Surface Pro 7");
        o.setCreateTime(new Date());
        String orderStr = JSONObject.toJSONString(o, SerializerFeature.WriteNullStringAsEmpty);
        return orderStr ;
    }


    @PostMapping
    public Map addOrder(@RequestBody String request){
        Order order = JSONObject.parseObject(request, Order.class);
        System.out.println(order.toString());
        Map m = new HashMap();
        m.put("data",order);
        return m;
    }

    @PutMapping
    public String updateOrder(@RequestBody OrderVO orderVO){
        //参数直接写 java bean 会被默认的json解析
        System.out.println(orderVO.toString());
        return "success";
    }

    @PutMapping("pay")
    public String payOrder(@RequestBody String request){
        OrderVO orderVO = JSONObject.parseObject(request, OrderVO.class);
        //拿到我想要的字段 直接一个pojo转换 或者直接 get属性 在继续向后面的service层传递
        System.out.println(orderVO.toString());
        return "fail";
    }
}
